"""
    Created By Khun on 2020/4/8
"""

__author__ = 'Khun'

from flask_wtf import FlaskForm
from wtforms import StringField, IntegerField, TextAreaField, SelectField, Form
from wtforms.validators import DataRequired, Length, NumberRange

from app.models import Types, Throw


def class_name(placeholder, _class='form-control'):
    render_kw = {
        'placeholder': placeholder,
        'class': _class,
    }
    return render_kw


class TypesForm(FlaskForm):
    """ 模板类型 """
    type_name = StringField('类型', validators=[DataRequired('请输入'), Length(1, 20)], render_kw=class_name('名字'))


class ThrowForm(FlaskForm):
    """ 模板管理 """

    def __init__(self, **kwargs):
        super(ThrowForm, self).__init__(**kwargs)
        type_id = Types.query.all()
        self.types_id.choices = [(i.tid, i.type_name) for i in type_id]

    temp_name = StringField('模板名称', validators=[DataRequired('请输入名称'), Length(2, 20)])
    temp_path = StringField('模板路径', validators=[DataRequired('请输入路径'), Length(2, 20)])
    path_url = StringField('外部链接')
    types_id = SelectField('模板类型', render_kw=class_name('模板类型'), coerce=int, default=True)


class ManageForm(FlaskForm):
    """ 投放管理 """

    def __init__(self, **kwargs):
        super(ManageForm, self).__init__(**kwargs)
        tem_id = Throw.query.order_by(Throw.tid.desc()).all()
        self.throw_tid.choices = [(i.tid, i.temp_name) for i in tem_id]

    web_url = StringField('投放域名', validators=[DataRequired('请输入投放的域名'), Length(1, 50)])
    wechat = StringField('投放微信', validators=[DataRequired('请输入微信')])
    throw_tid = SelectField('投放模板', render_kw=class_name('请选择'), coerce=int)
    cnzz = IntegerField('cnzz号', default=0)
    copyright_ = StringField('公司版权', default='')
    base_js = TextAreaField('基础转换代码')
    transfer = StringField('调用js')
    third_api = TextAreaField('第三方api')
    remark = TextAreaField('备注')


class SearchForm(Form):
    """ 搜索 """
    q = StringField(validators=[DataRequired(), Length(
        min=1, max=50, message='长度必须在2到20个字符之间')])
    page = IntegerField(validators=[NumberRange(min=1, max=99)], default=1)


